using RStudio.Elevator;
using RStudio.UIFramwork;
using System.Collections;
using UnityEngine;


/// <Summary>
///
/// </Summary>
[UIDetail("PageElevator", "PageElevator")]
public class PageElevator : UIBase
{
    /// <Summary>
    /// 本地组件集
    /// </Summary>
    public DetailPageElevator Detail;


    public override void InitComponent()
    {
        Detail = new DetailPageElevator(transform);

        this.Detail.TGL_ToggleF1.onValueChanged.AddListener(OnClickF1);
        this.Detail.TGL_ToggleF2.onValueChanged.AddListener(OnClickF2);
        this.Detail.TGL_ToggleF3.onValueChanged.AddListener(OnClickF3);



        var driven = ElevatorModuleCenter.Instance.ElevatorModuleDriven.Get(ElevatorType.Demo.ToString());
        driven.OnDrivenDeActive += OnArrived;
        driven.OnDrivenActive += e => Debug.Log("电梯启动了");


        StartCoroutine(UpdateTmp());
    }

    private void OnArrived(QueueEvent arg)
    {
        if (arg.Data.FloorNo == 3)
        { 
            this.Detail.TGL_ToggleF3.SetIsOnWithoutNotify(false);
            return;
        }
        if (arg.Data.FloorNo == 2)
        {
            this.Detail.TGL_ToggleF2.SetIsOnWithoutNotify(false);
            return;
        }
        if (arg.Data.FloorNo == 1)
        {
            this.Detail.TGL_ToggleF1.SetIsOnWithoutNotify(false);
            return;
        }
    }

    private void OnClickF1(bool arg0)
    {
        var call = ElevatorModuleCenter.Instance.ElevatorModuleCall.Get(ElevatorType.Demo.ToString());

        call.SendRequest(new DataFloorRequest()
        {
            FloorNo = 1,
            IsSelected = arg0,
            RequestType = ElevatorRequestType.Remote,
        });
    }
    private void OnClickF2(bool arg0)
    {
        var call = ElevatorModuleCenter.Instance.ElevatorModuleCall.Get(ElevatorType.Demo.ToString());

        call.SendRequest(new DataFloorRequest()
        {
            FloorNo = 2,
            IsSelected = arg0,
            RequestType = ElevatorRequestType.Remote,
        });
    }
    private void OnClickF3(bool arg0)
    {
        var call = ElevatorModuleCenter.Instance.ElevatorModuleCall.Get(ElevatorType.Demo.ToString());

        call.SendRequest(new DataFloorRequest()
        {
            FloorNo = 3,
            IsSelected = arg0,
            RequestType = ElevatorRequestType.Remote,
        });
    }


    private IEnumerator UpdateTmp()
    {
        while (true)
        {
            var driven = ElevatorModuleCenter.Instance.ElevatorModuleDriven.Get(ElevatorType.Demo.ToString());

            if (driven == null)
            {
                this.Detail.TextMeshProUGUI_ElevatorDetail.text = "123";
                yield return null;
            }

            this.Detail.TextMeshProUGUI_ElevatorDetail.text = $"" +
                $"State - working: {driven.StatusElevator}\n" +
                $"State - running: {driven.StatusRunning}\n" +
                $"Current FloorNo: {driven.CurrentFloorNo}\n";

            yield return null;
        }

    }
}
